﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgileZen.Models;

namespace AgileZen.Sample.Controllers
{
    public class RoleController : AgileZenController
    {
       
        public ActionResult Index(int projectid)
        {
            return View(AgileZenService.GetRoles(projectid));
            
        }

        public ActionResult Details(int id, int projectid)
        {
            return View(AgileZenService.GetRole(projectid, id));
        }

        public ActionResult Add(int projectid)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(int projectid, Role role)
        {
            try
            {
                AgileZenService.CreateRole(projectid, ref role);

                return RedirectToAction("Details", new { id = role.Id, projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(int projectid, int id)
        {

            try
            {
                return View(AgileZenService.GetRole(projectid, id));
            }
            catch
            {
                return View();
            }

        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(int projectid, int id, Role role)
        {
            try
            {
                // TODO: Add update logic here
                AgileZenService.UpdateRole(projectid, ref role);
                return RedirectToAction("Index", new { projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int projectid, int id)
        {
            try
            {
                AgileZenService.DeleteRole(projectid, id);

                return RedirectToAction("Index", new { projectid = projectid });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Members(int projectid, int id)
        {
            try
            {
                return View(AgileZenService.GetPeopleInRole(projectid,id));
            }
            catch
            {
                return View();
            }
        }

        public ActionResult AddMe(int projectid, int id)
        {
            try
            {
                var me = AgileZenService.GetMe();


                AgileZenService.AddPersonToRole(projectid, id, me);

               
                return View("Members",AgileZenService.GetPeopleInRole(projectid, id));
            }
            catch
            {
                return View();
            }
        }

    }
}
